package com.ds.infrastructure.audit.collector.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ds.infrastructure.audit.collector.entity.App;
import com.ds.infrastructure.audit.collector.service.IAppService;
import com.ds.infrastructure.audit.collector.vo.AppQueryVo;
import com.ds.infrastructure.audit.common.entity.Pager;
import com.ds.infrastructure.audit.common.entity.Result;
import com.ds.infrastructure.audit.common.validator.Save;
import com.ds.infrastructure.audit.common.validator.Update;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/7/30
 */
@RestController
@RequestMapping(value = "/app")
@Slf4j
public class AppController {

    @Resource
    private IAppService appService;


    @ApiOperation(value = "新增APP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping(value = "/save")
    @Secured("ROLE_ADMIN")
    public Result saveApp(@RequestBody @Validated(Save.class) App app, HttpServletRequest request) {
        //只有管理员可以调用
        appService.saveApp(app);
        return Result.success(app.getAppKey());
    }

    @PostMapping(value = "/queryWithPage")
    public Result queryWithPage(@RequestBody AppQueryVo vo) {
        Page page = PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(vo.getAppName()), "app_name", vo.getAppName())
                .like(StrUtil.isNotBlank(vo.getOwner()), "owner", vo.getOwner());
        List<App> list = appService.list(queryWrapper);
        return Result.success(new Pager<>(page.getTotal(), list));
    }

    Random random = new Random();

    //下面3种注解是等价的
    @RolesAllowed({"ADMIN"})
    @GetMapping(value = "/t1")
    public String t1() {
        return "t1-ADMIN-" + random.nextInt(10000);
    }

    @Secured({"ROLE_USER"})
    @GetMapping(value = "/t2")
    public String t2() {
        return "t2-USER-" + random.nextInt(10000);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping(value = "/t3")
    public String t3() {
        return "t3-" + random.nextInt(10000);
    }

    @RolesAllowed({"ROLE_ADMIN", "USER"})
    @GetMapping(value = "/t4")
    public String t4() {
        return "t4-" + random.nextInt(10000);
    }

    @RolesAllowed({"VISITOR"})
    @GetMapping(value = "/t5")
    public String t5() {
        return "t5-" + random.nextInt(10000);
    }

    @ApiOperation(value = "删除APP")
    @GetMapping(value = "/delete/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result delete(@PathVariable Long id) {
        return Result.success(appService.deleteAppById(id));
    }


    @ApiOperation(value = "修改APP", notes = "id不能为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping(value = "/update")
    @Secured("ROLE_ADMIN")
    public Result updateApp(@RequestBody @Validated(Update.class) App app) {
        return Result.success(appService.updateApp(app));
    }

}
